package com.yuanxin.player;

import android.app.Activity;
import android.content.SharedPreferences;
import android.util.Log;

import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.bridge.ReadableArray;
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.bridge.UiThreadUtil;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.common.MapBuilder;
import com.facebook.react.uimanager.annotations.ReactProp;
import com.facebook.react.uimanager.ThemedReactContext;
import com.facebook.react.uimanager.ViewGroupManager;
import com.facebook.react.uimanager.events.RCTEventEmitter;

import java.util.Map;

import javax.annotation.Nullable;


public class YuanXinPlayerViewManager extends ViewGroupManager<YuanXinPlayerView> {
    private static final String REACT_CLASS = "YuanXinPlayer";
    private YuanXinPlayerView rctijkPlayerView;
    private ReactContext reactContext;

    private final int Start_Code = 1;
    private final int Stop_Code = 2;
    private final int Pause_Code = 3;
    private final int Resume_Code = 4;
    private final int Shutdown_Code = 5;
    private final int SeekTo_Code = 6;
    private final int Restart_Code = 7;


    private SharedPreferences sp;
    private SharedPreferences.Editor edt;

//    private Activity activity = null;

//    public RCTIJKPlayerViewManager(Activity activity){
//        this.activity = activity;
//    }

    @Override
    public String getName() {
        return REACT_CLASS;
    }

    @Override
    public YuanXinPlayerView createViewInstance(ThemedReactContext context) {
        rctijkPlayerView = new YuanXinPlayerView(context);
        this.reactContext = context;
        return rctijkPlayerView;
    }

    @ReactProp(name = "url")
    public void setUrl(YuanXinPlayerView view, String Url) {
       // Log.e("TAG", "url---" + Url);
        sp = reactContext.getSharedPreferences("sp_player", Activity.MODE_PRIVATE);
        edt = sp.edit();
        edt.putString("player_url", Url);
        edt.apply();
    }

    @Nullable
    @Override
    public Map<String, Integer> getCommandsMap() {
        return MapBuilder.of(
                "start", Start_Code,
                "stop", Stop_Code,
                "pause", Pause_Code,
                "resume", Resume_Code,
                "shutdown", Shutdown_Code,
                "seekTo", SeekTo_Code
        );
    }

    @Nullable
    @Override
    public Map<String, Object> getExportedCustomDirectEventTypeConstants() {
        MapBuilder.Builder builder = MapBuilder.builder();
        builder.put("onCompletion", MapBuilder.of("registrationName", "onCompletion"));
        return builder.build();
    }

    @Override
    public void receiveCommand(YuanXinPlayerView root, int commandId, @Nullable ReadableArray args) {
        switch (commandId) {
            case Start_Code:
                start();
                break;
            case Stop_Code:
                stop();
                break;
            case Resume_Code:
                resume();
                break;
            case Pause_Code:
                pause();
                break;
            case Shutdown_Code:
                shutdown();
                break;
            case SeekTo_Code:
                ReadableMap map = args.getMap(0);
                double currentPlaybackTime = map.getDouble("currentPlaybackTime");
                seekTo(currentPlaybackTime);
                break;
        }
    }

    public void start() {
        sp = reactContext.getSharedPreferences("sp_player", Activity.MODE_PRIVATE);
        final String Url = sp.getString("player_url", null);
        YuanXinPlayer.getViewInstance().start(Url);
    }

    public void stop() {
        YuanXinPlayer.getViewInstance().stop();
    }

    public void pause() {
        YuanXinPlayer.getViewInstance().pause();
    }

    public void resume() {
        YuanXinPlayer.getViewInstance().resume();
    }

    public void shutdown() {
        YuanXinPlayer.getViewInstance().shutdown();
    }

    public void seekTo(double currentPlaybackTime) {
        YuanXinPlayer.getViewInstance().seekTo(currentPlaybackTime);
    }

    public void setError(YuanXinPlayerView view, String error) {
        WritableMap event = Arguments.createMap();
        event.putString("currentError", error);
        ReactContext reactContext = (ReactContext) view.getContext();
        reactContext.getJSModule(RCTEventEmitter.class).receiveEvent(
                view.getId(),
                "onError",
                event);
    }

}
